package server.version;

import servlet.Request;
import servlet.Response;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created by louisyuu on 2020/4/2 上午11:03
 * <p>
 * <p>
 * <p>
 * 完成Minicat 2.0版本
 * 需求：封装Request和Response对象，返回html静态资源文件
 */
public class VersionTwoStarter implements VersionStater {


    @Override
    public void start() throws Exception {

        /**
         * 完成Minicat 2.0版本
         * 需求：封装Request和Response对象，返回html静态资源文件
         */
        ServerSocket serverSocket = new ServerSocket(port);
        System.out.println("=====>>>Minicat start on port：" + port);


        /**
         * while true 一直监听
         */
        while (true) {
            //启动监听
            Socket socket = serverSocket.accept();
            InputStream inputStream = socket.getInputStream();

            // 封装Request对象和Response对象
            Request request = new Request(inputStream);
            Response response = new Response(socket.getOutputStream());

            response.outputHtml(request.getUrl());
            socket.close();

        }
    }
}
